package com.globallogic.jbtask2;

import java.util.Scanner;

public class Arrays {
    
    private static final int ARRAY_SIZE = 10;    

    private int getMax(int[] intArray) {

        int max = 0;
        
        for (int i = 0; i < ARRAY_SIZE; i++) {

    
            if (intArray[i] > max) {
            
                max = intArray[i];  

            }
            
        }
 
        return max;
    
    }

    private int[] getIntArray() {

        Scanner input = new Scanner(System.in);
        int[] intArray = new int[ARRAY_SIZE]; 
        
        String inputStr = "";
        int intValue;
        int i = 0;

        while (i < ARRAY_SIZE) {

            inputStr = input.next();
            
            try {

                intValue = Integer.parseInt(inputStr);
                intArray[i] = intValue;
                i++;

            } catch (NumberFormatException e) {

                System.out.println("Entered value is not a number. Try once more, please.");
                            
  
            }
        }

        return intArray;
    
    }


    public int getMinDistance(int[] intArray) {

        int minDistance;
        int distance;

        minDistance = getMax(intArray);

        for (int i = 0; i < ARRAY_SIZE; i++) {

            for (int j = 0; j < ARRAY_SIZE; j++) {
             
                if (j == i) {
                  continue;
                }
              
                distance = Math.abs(intArray[i] - intArray[j]);

                if (distance < minDistance) {
                  
                    minDistance = distance;
              
                }
            }
 
        }

        return minDistance;

    }

    public String getArrayAsString(int[] intArray) {

        StringBuilder sb = new StringBuilder();

        for (int i: intArray) {
        
            sb.append(i + " ");
  
        }

        return sb.toString();
    
    }
 
    public static void main(String[] args) {

        int minDistance;
        int[] intArray;
        Arrays arraysApp = new Arrays();

        System.out.println("Please, enter " + ARRAY_SIZE + " integers");
 
        intArray = arraysApp.getIntArray();
        minDistance = arraysApp.getMinDistance(intArray); 

        System.out.print("Entered array is: " + arraysApp.getArrayAsString(intArray));
        System.out.println();
        System.out.println("Min distance is: " + minDistance);

    }

}